﻿//-----------------------------------------------------------------------
// <copyright file="ContractHelper.cs" company="P.O.S Informatique">
//     Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace PosInformatique.Framework
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Diagnostics.Contracts;
    using System.Linq;

    /// <summary>
    /// Contains contracts abbreviator validation helper
    /// </summary>
    [ExcludeFromCodeCoverage]
    internal static class ContractHelper
    {
        /// <summary>
        /// Determines if the specified <paramref name="value"/> is not null.
        /// </summary>
        /// <typeparam name="T">The value type to test.</typeparam>
        /// <param name="value">The value to test.</param>
        [ContractAbbreviator]
        public static void IsNotNull<T>(T value)
            where T : class
        {
            Contract.Requires<ArgumentNullException>(value != null);
        }

        /// <summary>
        /// Check if the method return a non null value.
        /// </summary>
        /// <typeparam name="T">Value type returned by the method which must not return null.</typeparam>
        [ContractAbbreviator]
        public static void ReturnNonNull<T>()
            where T : class
        {
            Contract.Ensures(Contract.Result<T>() != null);
        }

        /// <summary>
        /// Check if the method returns a <see cref="IEnumerable{T}"/> which non null elements.
        /// </summary>
        /// <typeparam name="T">Nullable elements of the returned <see cref="IEnumerable{T}"/>.</typeparam>
        [ContractAbbreviator]
        public static void ReturnItemsNonNull<T>()
            where T : class
        {
            Contract.Ensures(Contract.Result<IEnumerable<T>>() != null);
            Contract.Ensures(Contract.ForAll(Contract.Result<IEnumerable<T>>(), item => item != null));
        }
    }
}
